/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ListaCategoria.java
 *
 * Created on 24/11/2010, 01:54:39 AM
 */

package pe.edu.upla.lamerced.desk.view.maestro;

import java.util.List;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.springframework.beans.factory.annotation.Autowired;
import pe.edu.upla.lamerced.Inicial;
import pe.edu.upla.lamerced.bean.Categoria;
import pe.edu.upla.lamerced.service.CategoriaService;

/**
 *
 * @author elvis
 */
public class ListaCategoria extends javax.swing.JInternalFrame {

   private JDesktopPane escritorio;
   private Categoria categoria;
   private CategoriaService categoriaService;
   private List<Categoria> listaCategorias;

    /** Creates new form ListaCategoria */
    public ListaCategoria() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {

      jScrollPane1 = new javax.swing.JScrollPane();
      tblCategorias = new javax.swing.JTable();
      jButton1 = new javax.swing.JButton();
      jButton2 = new javax.swing.JButton();
      jButton3 = new javax.swing.JButton();
      jLabel1 = new javax.swing.JLabel();
      txtNombre = new javax.swing.JTextField();
      btnGrabar = new javax.swing.JButton();
      jLabel2 = new javax.swing.JLabel();

      setClosable(true);
      setTitle("Lista Categorias");

      tblCategorias.setModel(new javax.swing.table.DefaultTableModel(
         new Object [][] {
            {null, null}
         },
         new String [] {
            "Codigo", "Nombre"
         }
      ));
      jScrollPane1.setViewportView(tblCategorias);

      jButton1.setText("Nuevo");
      jButton1.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
         }
      });

      jButton2.setText("Modificar");
      jButton2.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
         }
      });

      jButton3.setText("Eliminar");
      jButton3.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton3ActionPerformed(evt);
         }
      });

      jLabel1.setText("Categoria");

      btnGrabar.setText("Grabar");
      btnGrabar.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnGrabarActionPerformed(evt);
         }
      });

      jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14));
      jLabel2.setText("Mantenimiento de Categorias");

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addGap(29, 29, 29)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(layout.createSequentialGroup()
                  .addComponent(jLabel1)
                  .addGap(18, 18, 18)
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)
                     .addGroup(layout.createSequentialGroup()
                        .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnGrabar)))
                  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
               .addGroup(layout.createSequentialGroup()
                  .addComponent(jButton1)
                  .addGap(56, 56, 56)
                  .addComponent(jButton2)
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 71, Short.MAX_VALUE)
                  .addComponent(jButton3)
                  .addGap(47, 47, 47))))
         .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE)
      );
      layout.setVerticalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addContainerGap(31, Short.MAX_VALUE)
            .addComponent(jLabel2)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(jLabel1)
               .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
               .addComponent(btnGrabar))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(jButton1)
               .addComponent(jButton2)
               .addComponent(jButton3))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
      );

      pack();
   }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
       if (tblCategorias.getSelectedRow()==-1){
          JOptionPane.showMessageDialog(this, "No ha seleccionado una categoria");
         return;
       }
       categoria = listaCategorias.get(tblCategorias.getSelectedRow());
       txtNombre.setText(categoria.getNombre());
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
       // TODO add your handling code here:
       categoria = new Categoria();
       txtNombre.setText("");
       txtNombre.requestFocusInWindow();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
       if (tblCategorias.getSelectedRow()==-1){
          JOptionPane.showMessageDialog(this, "No ha seleccionado una categoria");
         return;
       }
       int res = JOptionPane.showConfirmDialog(this,"Seguro que desea eliminar la categoria?","Eliminar Categoria",JOptionPane.YES_NO_OPTION);
       if(res==0){
         Categoria cat = listaCategorias.get(tblCategorias.getSelectedRow());
         cat.setEstado(0);
         categoriaService.registrarCategoria(cat);
         JOptionPane.showMessageDialog(this, "Se ha eliminado la categoria");
         listaCategorias = categoriaService.listarCategorias();
         pintagrilla();
         txtNombre.setText("");
       }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void btnGrabarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGrabarActionPerformed
       // TODO add your handling code here:
       try {

           if(categoria == null){
                categoria = new Categoria();
           }

         categoria.setNombre(txtNombre.getText().trim());
         categoria.setEstado(1);
         categoriaService.registrarCategoria(categoria);
         JOptionPane.showMessageDialog(this, "Categoria registrada con exito");
         listaCategorias = categoriaService.listarCategorias();
         pintagrilla();
         categoria = new Categoria();
         txtNombre.setText("");
       } catch (Exception e) {
          JOptionPane.showMessageDialog(this, "A ocurrido un error: " + e.getMessage(),"Error en registro",JOptionPane.ERROR_MESSAGE);
       }
       
    }//GEN-LAST:event_btnGrabarActionPerformed


   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JButton btnGrabar;
   private javax.swing.JButton jButton1;
   private javax.swing.JButton jButton2;
   private javax.swing.JButton jButton3;
   private javax.swing.JLabel jLabel1;
   private javax.swing.JLabel jLabel2;
   private javax.swing.JScrollPane jScrollPane1;
   private javax.swing.JTable tblCategorias;
   private javax.swing.JTextField txtNombre;
   // End of variables declaration//GEN-END:variables

   public JDesktopPane getEscritorio() {
      return escritorio;
   }
   public void setEscritorio(JDesktopPane escritorio) {
      this.escritorio = escritorio;
   }

   public void cargainicial(){
      if(categoriaService==null){
         categoriaService = (CategoriaService)Inicial.getFactory().getBean("categoriaService");
      }
      if(listaCategorias==null){
         listaCategorias = categoriaService.listarCategorias();
      }
      pintagrilla();
   }

   public void pintagrilla(){
        DefaultTableModel tabla = (DefaultTableModel) tblCategorias.getModel();
        tabla.setRowCount(0);
        for (Categoria dfac : listaCategorias) {
            Object[] fila = new Object[4];
            fila[0] = dfac.getIdcategoria().toString();
            fila[1] = dfac.getNombre();
            tabla.addRow(fila);
        }

      }

}
